package com.ice.ubt.service;

import android.util.Log;
import android.view.View;

import com.ice.ubt.App;
import com.ice.ubt.R;
import com.ice.ubt.event.UBTEvent;
import com.ice.ubt.event.UBTEventDao;
import com.ice.ubt.greendao.DbCore;

import java.util.Date;

public class AddEventThread implements Runnable {
    private String action;
    private View view;
    private String pageName;
    private String action_value;
    private UBTEventDao ubtEventDao;

    public AddEventThread(String action, View view, String pageName, String action_value) {
        this.action = action;
        this.view = view;
        this.pageName = pageName;
        this.action_value = action_value;
        ubtEventDao = DbCore.getDaoSession().getUBTEventDao();
    }

    @Override
    public void run() {
        UBTEvent event = new UBTEvent();
        event.object = view.getClass().getSimpleName();
        event.action = action;
        event.ts = new Date().getTime();
        event.obj_name = (String) view.getTag(R.id.UBT_TAG);
        event.page = pageName;
        if (action_value != null)
            event.action_value = action_value;
        Log.e("UBT......", event .toString());
        if (ubtEventDao.insertOrReplace(event) != -1) {
            long count =ubtEventDao.queryBuilder().count();
            Log.e("UBT......", "数据库插入成功..."+"总数："+count);
            if (count==10){
                App.getInstance().uploadDataService(App.getInstance());
            }
        }
    }
}
